//
//  ViewController.swift
//  MySwiftProject
//
//  Created by 蓓蕾 on 2021/11/5.
//

import UIKit
import Alamofire
import SnapKit
import SwiftyJSON
import HandyJSON
import SwiftUI



///// 结构体model 来接收得到的jsonString
//struct responseModel: HandyJSON {
//
//    var data:[TagModel]?
//    var error: Int!
//}
//struct TagModel: HandyJSON {
//
//    var nickname: String?
//    var vertical_src: String?
//    var ranktype: String?
//    var room_src: String?
//    var cate_id: Int?
//}
//class ViewController: UIViewController {
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    private lazy var netData = NSDictionary()
    private lazy var banner = NSMutableArray()
    var cyclePictureView : JCyclePictureView!
    var tableView : UITableView!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.orange;
        self.initUI()
        self.getNetData()
        //        self.getData()
    }
    
    func initUI()
    {
        let headerView = UIView(frame:CGRect(x: 0, y: 0, width: kScreenWidth, height: 800))
        
        let pictures:[String] = [ImagePrefix+"1c3894b8-63ae-11eb-bf87-00163e08b39e.png", ImagePrefix+"aebe8350-7e58-11eb-8c9c-00163e08b39e.png", ImagePrefix+"0af9bd72-e6a8-11ea-ab35-00163e08b39e.png"];
        self.cyclePictureView = JCyclePictureView(frame: CGRect(x: 0, y: 20, width: kScreenWidth, height: 200), pictures: pictures)
        headerView.addSubview(cyclePictureView)
        
        let topItemView = HomeTopItemView()
        headerView.addSubview(topItemView)
        topItemView.topImageView.image = UIImage.init(systemName: "点滴")
        topItemView.titleView.text = "点滴"
        topItemView.snp.makeConstraints { make in
            make.left.equalTo(self.view.snp_left).offset(20)
            make.top.equalTo(cyclePictureView.snp_bottom).offset(10)
            make.width.equalTo(80)
            make.height.equalTo(80)
        }
        
        
        
        
        // 点击回调
        cyclePictureView.didTapAtIndexHandle = { index in
            
            print("点击了第 \(index + 1) 张图片")
        }
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight), style: .plain)
        self.view.addSubview(self.tableView)
        self.tableView.tableHeaderView = headerView
        self.tableView.delegate = self as UITableViewDelegate
        self.tableView.dataSource = self as UITableViewDataSource
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "UITableViewCell")
//        self.tableView.delegate.self
        //        let imageView = UIImageView(frame: CGRect(x: 10, y: cyclePictureView.frame.origin.y + 20, width: kScreenWidth - 20, height: 500))
        //        self.view.addSubview(imageView)
        //        imageView.backgroundColor = UIColor.white;
//        cyclePictureView.backgroundColor = UIColor.hexColorWithAlpha(color: <#T##String#>, alpha: <#T##CGFloat#>)
//        cyclePictureView.backgroundColor = UIColor.hexColor(color: "0x")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")!;
        cell.textLabel?.text = String(format: "\(indexPath.row)")
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 100;
    }
    
    func getNetData()
    {
        let parama = ["uid":"5449","customer_type":"兑换码用户"]
        RequestManager.shareInstance.postRequest(UrlString: Circle3, parama: parama) { result in
            self.netData = result.result.value as! NSDictionary
            CWLog("result = \(String(describing: self.netData))")
            let value = self.netData.object(forKey: "values") as! NSDictionary
            let banner = value.object(forKey: "banner") as! NSArray
            banner.forEach({ (bannerDic) in
                let bannerItem = bannerDic as! NSDictionary
                let imageName = bannerItem.object(forKey: "image")  as! NSString
                let imageUrl = String(format: "%@%@", ImagePrefix,imageName)// ImagePrefix.append(imageName as! String)
                self.banner.add(imageUrl)
            })
            self.cyclePictureView.pictures = self.banner as! [String];
            
            //            print(banner.description.stringByReplaceUnicode)
            //            print(self.netData.description.stringByReplaceUnicode)
            self.detailData()
            
            //            let values = result.data?;
            //            CWLog(result)
            
            
            //            if let jsonString = result.result.value {
            //
            //                /// json转model
            //                /// 写法一：responseModel.deserialize(from: jsonString)
            //                /// 写法二：用JSONDeserializer<T>
            ////                if let responseModel = JSONDeserializer<responseModel>.deserializeFrom(json: jsonString) {
            //
            //                    /// model转json 为了方便在控制台查看
            ////                        print(responseModel.toJSONString(prettyPrinted:true)!)
            ////
            ////
            ////                            print(responseModel.toJSONString(prettyPrint: true)!)
            ////
            ////                            /// 遍历responseModel.data
            //                    responseModel.data?.forEach({ (model) in
            //                        print(model.nickname!);
            //                    })
            //
            ////                }
            //            }
            
            
            
            
            //            let jsonDirection = JSON(result);
            //            let message = jsonDirection["message"];
            //            let values = jsonDirection["values"]
            //            let json = JSON(result).dictionary
            
            //            let H:HomeModel = HomeModel.deserialize(from: result, designatedPath: "")
            //            CWLog(message)
            //            CWLog(json)
            //            print("成功\(result)")
        } failure: { failure in
            //            print("失败\(failure)")
        }
        
    }
    
    func detailData(){
        //        print("处理数据\(String(describing: self.netData))")
        
    }
    
    //    fileprivate func getData() {
    //
    ////            let recommend_collectionurl = "http://capi.douyucdn.cn/api/v1/getbigDataRoom?aid=ios&client_sys=ios&time=1468636740&token=30890623_1b036814902f6451&auth=7d7026a323e09dd55c71ca215fc9d4b2"
    //        let recommend_collectionurl = "https://www.titaids.com/api/text/one/circlev3"
    //            Alamofire.request(recommend_collectionurl, method: .post, parameters: nil, encoding: URLEncoding.default).responseJSON { (response) in
    ////                CWLog(response)
    //                if response.result.isSuccess {
    //                    let jsonString = response.result.value
    //                    CWLog(jsonString)
    //                    var netDic = NSDictionary()
    //                    netDic = jsonString as! NSDictionary;
    //                    CWLog(netDic)
    //                    let value = netDic.object(forKey: "values")
    //                    CWLog("values = \(value)")
    //                    let banner = netDic.object(forKey: "banner")
    ////                    if let jsonString = response.result.value {
    ////
    ////                        /// json转model
    ////                        /// 写法一：responseModel.deserialize(from: jsonString)
    ////                        /// 写法二：用JSONDeserializer<T>
    //////                        if let responseModel = JSONDeserializer<HomeModel>.deserializeFrom(json: jsonString as! String) {
    ////
    ////                            /// model转json 为了方便在控制台查看
    //////                        print(responseModel.toJSONString(prettyPrinted:true)!)
    //////
    //////
    //////                            print(responseModel.toJSONString(prettyPrint: true)!)
    //////
    //////                            /// 遍历responseModel.data
    ////                        jsonString.values?.forEach({ (model) in
    ////                                print(model.nickname!);
    ////                            })
    ////
    ////                        }
    ////                    }
    //                }
    //            }
    //        }
    
    
    
}

